package cn.sdormitory.controller.basedata;

import cn.sdormitory.basedata.entity.ClassDevice;
import cn.sdormitory.basedata.entity.BDevice;
import cn.sdormitory.basedata.entity.DorDevice;
import cn.sdormitory.basedata.service.BDeviceService;
import cn.sdormitory.common.api.CommonPage;
import cn.sdormitory.common.api.CommonResult;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author: lx
 * @date: 2023/8/3 16:57
 * @description: 设备管理
 */
@RestController
@Api(tags = "Basedata-bDevice=> 设备管理")
@RequestMapping("/basedata/bDevice")
public class BDeviceController {

    @Autowired
    BDeviceService bDeviceService;

    @PostMapping("/findPageList")
    public CommonResult<CommonPage> findPageList(@RequestBody BDevice device){
        IPage<BDevice> list = bDeviceService.findPageList(device);
        return CommonResult.success(CommonPage.restPage(list));
    }

    @PostMapping("/getDeviceInfoFromIotSys")
    public void getDeviceInfoFromIotSys(@RequestBody List<Map<String,String>> deviceList){
        bDeviceService.getDeviceInfoFromIotSys(deviceList);
    }

    @PostMapping("/addOrUpdate")
    public CommonResult addOrUpdate(@RequestBody BDevice device){
        int num = bDeviceService.addOrUpdate(device);
        if(num > 0){
            return CommonResult.success();
        }else {
            return CommonResult.failed();
        }
    }

    @GetMapping("/findById/{id}")
    public CommonResult findById(@PathVariable("id") Long id){
        return CommonResult.success(bDeviceService.findById(id));
    }

    @ApiOperation("bindClass => 绑定班级")
    @PostMapping("/bindClass")
    public CommonResult bindClass(@RequestBody ClassDevice classDevice){
        int num = bDeviceService.bindClass(classDevice);
        if(num > 0){
            return CommonResult.success();
        }else {
            return CommonResult.failed("当前设备或班级存在绑定关系");
        }
    }

    @ApiOperation("unbindClass => 解绑班级")
    @GetMapping("/unbindClass/{deviceId}")
    public CommonResult unbindClass(@PathVariable("deviceId") Long deviceId){
        int num = bDeviceService.unbindClass(deviceId);
        if(num > 0){
            return CommonResult.success();
        }else {
            return CommonResult.failed("解绑失败");
        }
    }

    @ApiOperation("bindDor => 绑定宿舍")
    @PostMapping("/bindDor")
    public CommonResult bindDor(@RequestBody DorDevice dorDevice){
        int num = bDeviceService.bindDor(dorDevice);
        if(num > 0){
            return CommonResult.success();
        }else {
            return CommonResult.failed("当前设备或班级存在绑定关系");
        }
    }

    @ApiOperation("unbindDor => 解绑宿舍")
    @GetMapping("/unbindDor/{deviceId}")
    public CommonResult unbindDor(@PathVariable("deviceId") Long deviceId){
        int num = bDeviceService.unbindDor(deviceId);
        if(num > 0){
            return CommonResult.success();
        }else {
            return CommonResult.failed("解绑失败");
        }
    }

    @ApiOperation("delete => 删除")
    @PostMapping("/delete")
    public CommonResult delete(@RequestBody Map<String,String> map){
        boolean flag = bDeviceService.delete(map);
        if(flag){
            return CommonResult.success();
        }else {
            return CommonResult.failed("删除失败");
        }
    }


}
